package com.hexu.event.dao;

import com.hexu.event.entity.Administrator;
import com.hexu.event.entity.User;
import com.hexu.event.util.Base64Util;
import com.hexu.event.util.JdbcUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class AdministratorDao {
    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;

    //增加管理员
    public boolean addAdministrator(Administrator administrator){
        String sql = "insert into administrator(account,password)"
                + " values (?,?)";
        try {
            conn = JdbcUtil.getConn();
            ps = conn.prepareStatement(sql);
            //Base64 加密
            //View层set的数据
            ps.setString(1,administrator.getAccount());
            ps.setString(2, Base64Util.encrypt(administrator.getPassword()));
            ps.executeUpdate();

            //添加成功
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {

            JdbcUtil.close(conn, ps, null);
        }
        //添加失败
        return false;
    }

    //管理员登录验证
    public boolean loginAdministrator(String account,String password){
        String sql="select * from administrator where account=? and password=?";

        try {
            conn = JdbcUtil.getConn();
            ps = conn.prepareStatement(sql);
            ps.setString(1, account);
            ps.setString(2,Base64Util.encrypt(password));
            rs = ps.executeQuery();

            while(rs.next()){
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JdbcUtil.close(conn, ps, rs);
        }
        return false;
    }

    //注销管理员
    public boolean deleteAdministrator(String account){
        String sql="delete from administrator where account=?";
        try {
            conn = JdbcUtil.getConn();
            ps = conn.prepareStatement(sql);
            ps.setString(1,account);
            if(ps.executeUpdate()>0){
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JdbcUtil.close(conn, ps, null);
        }
        return false;
    }

    //修改管理员信息
    public boolean updateAdministrator(Administrator administrator){
        String sql = "update administrator set password=? where account=?";

        try {
            conn = JdbcUtil.getConn();
            ps = conn.prepareStatement(sql);
            //View层set的数据
            ps.setString(1, Base64Util.encrypt(administrator.getPassword()));
            ps.setString(2,administrator.getAccount());
            ps.executeUpdate();

            //修改成功
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {

            JdbcUtil.close(conn, ps, null);
        }

        //修改失败
        return false;
    }

    //读取单个管理员信息
    public Administrator getAdministrator(String account){
        String sql = "select * from administrator where account=?";
        try {
            conn = JdbcUtil.getConn();
            ps = conn.prepareStatement(sql);
            ps.setString(1,account);
            rs = ps.executeQuery();
            Administrator administrator = new Administrator();
            while (rs.next()){
                administrator.setId(rs.getInt("id"));
                administrator.setPassword(Base64Util.decode(rs.getString("password")));
                administrator.setAccount(account);
            }
            return administrator;

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            JdbcUtil.close(conn, ps, rs);
        }
        return null;
    }
}
